import { ReactElement } from 'react'
import { Form, Col } from 'antd'
import { ConditionSchema } from '@components/schema/condition'
import UnitNumberInput from '@src/components/unitNumberInput'
const { Item } = Form

function Main (): ReactElement {
  return (
    <>
      <Col span={8}>
        <Item label='总标的最小标的' name='dispute_amount_all_min'>
          <UnitNumberInput placeholder='请输入' unit='元' precision={2} />
        </Item>
      </Col>
      <Col span={8}>
        <Item label='总标的最大标的' name='dispute_amount_all_max'>
          <UnitNumberInput placeholder='请输入' unit='元' precision={2} />
        </Item>
      </Col>
    </>
  )
}

export function initFormat (f: { [k: string]: any }, t: { [k: string]: any }): void {
  if (typeof f.dispute_amount_all_min === 'string') {
    t.dispute_amount_all_min = f.dispute_amount_all_min
  }
  if (typeof f.dispute_amount_all_max === 'string') {
    t.dispute_amount_all_max = f.dispute_amount_all_max
  }
}

export function changeFormat (d: { [k: string]: any }): void {
  if (typeof d.dispute_amount_all_min === 'string' && /^\d+$/.test(d.dispute_amount_all_min)) {
    d.dispute_amount_all_min = parseInt(d.dispute_amount_all_min)
  }
  if (typeof d.dispute_amount_all_max === 'string' && /^\d+$/.test(d.dispute_amount_all_max)) {
    d.dispute_amount_all_max = parseInt(d.dispute_amount_all_max)
  }
}

export function getCondition (d: { [k: string]: any }): ConditionSchema[] {
  const condition: ConditionSchema[] = []
  if (typeof d.dispute_amount_all_min === 'string' && /^\d+(\.\d{0,2})?$/.test(d.dispute_amount_all_min)) {
    condition.push({ k: 'dispute_amount_all', o: '>=', v: d.dispute_amount_all_min })
  }
  if (typeof d.dispute_amount_all_max === 'string' && /^\d+(\.\d{0,2})?$/.test(d.dispute_amount_all_max)) {
    condition.push({ k: 'dispute_amount_all', o: '<=', v: d.dispute_amount_all_max })
  }
  return condition
}

export default {
  component: Main,
  initFormat,
  changeFormat,
  getCondition
}
